<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>gap</title>
  <script src="../../vue.js"></script>
</head>
<body>
  <div id="root">
    <Comp2 ref="comp2"></Comp2>
  </div>
  <script>
    Vue.config.productionTip = false

    const Comp1 = {
      template: `
        <div>component 1 {{address}}</div>
      `,
      inject: ['address'],
      mounted() {
        console.log(this.$root.username)
        console.log(this.$parent.token)
      },
    }
    const Comp2 = {
      template: `
        <div ref="div"><Comp1></Comp1></div>
      `,
      inject: ['address'],
      data() {
        return {
          token: 'abc123'
        }
      },
      components: {
        Comp1
      },
      mounted() {
        console.log(this.$refs['div'])
      },
    }
    var vm = new Vue({
      el: '#root',
      data: {
        username: 'admin'
      },
      components: {
        Comp2
      },
      provide() {
        return {
          address: '北京市昌平区沙河'
        }
      },
      mounted() {
        console.log(this.$refs['comp2'].token)
      },
    })
  </script>
</body>
</html>